//
//  LJP_ThirdViewController.m
//  God_is_a_Girl
//
//  Created by 李继鹏 on 15/10/4.
//  Copyright (c) 2015年 呸. All rights reserved.
//

#import "LJP_ThirdViewController.h"
#import <AFNetworking.h>
#import "LJP_GuideHeadModel.h"
#import "LJP_GuideHeadView.h"
#import "LJP_SecondGuideCell.h"
#import "LJP_GuideCellModel.h"
#import "LJP_PlayerViewController.h"
#import "LJP_PlayerView.h"
@interface LJP_ThirdViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) LJP_GuideHeadModel *headModel;
@property (nonatomic, strong) NSMutableArray *ListArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) LJP_PlayerView *playerView;
@end

@implementation LJP_ThirdViewController
- (void)tableViewSubview
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) style:1];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.ListArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJP_SecondGuideCell *cell = [tableView dequeueReusableCellWithIdentifier:@"xxx"];
    if (cell == nil) {
        cell = [[LJP_SecondGuideCell alloc] initWithStyle:0 reuseIdentifier:@"xxx"];
    }
    cell.model = self.ListArray[indexPath.row];
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    LJP_GuideHeadView *headView = [[LJP_GuideHeadView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 150)];
    headView.model = self.headModel;
    return headView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 150;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJP_PlayerViewController *player = [[LJP_PlayerViewController alloc] init];
    player.headModel = self.ListArray[indexPath.row];
    player.ListArray = self.ListArray;
    player.num = indexPath.row;
    Player *play = [Player share];
    play.listArray = self.ListArray;
    [play playWithUrl:[NSURL URLWithString:[self.ListArray[indexPath.row] playUrl64]] num:indexPath.row];
    [self presentViewController:player animated:YES completion:^{
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center postNotificationName:@"create" object:[NSNumber numberWithInteger:indexPath.row]];
    }];
}
- (void)dataHandle
{
    self.ListArray = [NSMutableArray array];
    NSString *string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/20?albumId=%@&pageSize=20&isAsc=true&device=android", self.albumId, self.albumId];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *HeadDic = [NSDictionary dictionary];
        HeadDic = responseObject[@"album"];
        self.headModel = [[LJP_GuideHeadModel alloc] init];
        [self.headModel setValuesForKeysWithDictionary:HeadDic];
        NSArray *array = responseObject[@"tracks"][@"list"];
        for (NSDictionary *dic in array) {
            LJP_GuideCellModel *model = [[LJP_GuideCellModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.ListArray addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.tName;
    [self dataHandle];
    [self tableViewSubview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
